
shell=/bin/bash
CURDIR=$(shell pwd)
CONFIG_FILE=$(CURDIR)/../config/config_all.mk
BASEDIR=$(CURDIR)/../

include $(CONFIG_FILE)
include Object.mk

OBJS= $(obj-cm-y:%=$(OBJDIR)/%)
OBJS_cpp=$(obj-cm-cpp-y:%=$(OBJDIR)/%)
OBJ_DEPS=$(obj-dep-y)
## form *.c to *(exec demo name)
DEMO_EXEC=$(patsubst %.c,%,$(obj-demo-y))
DEMO_EXEC_cpp=$(patsubst %.cpp,%,$(obj-demo-cpp-y))
DEMO_EXEC_GEN=$(DEMO_EXEC:%=%-$(OS))

CFLAGS += $(CFLAGS-cm-y)
CXXFLAGS += $(CXXFLAGS-cm-y)
LDFLAGS += $(LDFLAGS-cm-y)

DEMO_CFLAGS += -I.

.PHONY:

BUILDIN=$(OBJDIR)/build-in.o

all: deps $(BUILDIN)

deps:
	@for i in $(predirs-y); \
		do \
	  	mkdir -p $(OBJDIR)/$$i; \
		done

test: $(DEMO_EXEC) $(DEMO_EXEC_cpp)

$(OBJS):$(OBJDIR)/%.o:%.c
	@echo "\t $(CC) \t $^"
	$(Q) $(CC) -o $@ -c $^ $(CFLAGS) $(LINKFLAGS)

$(OBJS_cpp):$(OBJDIR)/%.o:%.cpp
	@echo "\t $(CXX) \t $^"
	$(Q) $(CXX) -o $@ -c $^ $(CFLAGS) $(CXXFLAGS) $(LINKFLAGS)

$(BUILDIN): $(OBJS) $(OBJS_cpp)
	$(Q) $(LD) $(LDFLAGS) -r -o $@ $^

$(DEMO_EXEC):$(BUILDIN) $(obj-demo-y)
	@echo "\t $(CC) \t $@"
	$(Q) $(CC) -o $@ $(patsubst %,%.c,$@) $(BUILDIN) $(OBJ_DEPS) $(CFLAGS) $(DEMO_CFLAGS) $(LDFLAGS) $(LINKFLAGS) $(LINKPATH)

$(DEMO_EXEC_cpp):$(BUILDIN) $(obj-demo-cpp-y)
	@echo "\t $(CXX) \t $@"
	$(Q) $(CXX) -o $@ $(patsubst %,%.cpp,$@) $(BUILDIN) $(OBJ_DEPS) $(CFLAGS) $(CXXFLAGS) $(DEMO_CFLAGS) $(LDFLAGS) $(LINKFLAGS) $(LINKPATH)

clean:
	rm -rf $(OBJS) $(OBJS_cpp) $(BUILDIN)
	rm -rf $(DEMO_EXEC) $(DEMO_EXEC_cpp) $(DEMO_EXEC_GEN)

distclean:
	rm -rf $(OBJDIR)

